package mercede.allManagerBeans;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import mercede.allTable.Hotel;
import mercede.allDTOs.HotelDTO;
import mercede.allManagers.HotelManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Session Bean implementation class HotelManagerBean
 */
@Stateless
public class HotelManagerBean implements HotelManager {

	@PersistenceContext
	   private EntityManager em;
		
		@Resource
		private EJBContext context;
		

	    @Override
	    public void save(HotelDTO hoteldto) {
	            Hotel newHotel= new Hotel(hoteldto);
	            em.persist(newHotel);
	    }
	    
	       
	    @Override
	    public void remove(int hid) {
	 		Hotel hotel = find(hid);
	         em.remove(hotel);
	 	}
	     
	     public void remove(Hotel hotel) {
	     	em.remove(hotel);
	 	}
	     
	   
	   private Hotel find(int hid) {
	   return em.find(Hotel.class, hid);
	   }
	    
	   @Override
	   public ArrayList<?> getHotel() {
	   return (ArrayList<?>) em.createNamedQuery("Hotel.findAll", Hotel.class).getResultList();
	   }
	   
	   
	   @Override
	   public HotelDTO getHotelDTO(int hid) {
		   Hotel newHotel =find(hid);
		   HotelDTO hoteldto = convertToDTO(newHotel);
	       return hoteldto;
	   }
	   

	   @Override
	   public List<HotelDTO> getListHotelDTO() {
	       List<Hotel> newHotel= em.createNamedQuery("Hotel.findAll", Hotel.class).getResultList();
	       List<HotelDTO> hoteldto = new ArrayList<>();
	       for(Hotel h: newHotel) {
	    	   hoteldto.add(convertToDTO(h));
	       }
	       return hoteldto;
	   }
	   
	   

	   @Override
	   public List<HotelDTO> getListHotelDTOByDest(String hcity) {
		   
	      Query query=em.createNamedQuery("Hotel.FIND_BY_DEST", Hotel.class);
	      query.setParameter("hcity",hcity);
		   List<Hotel> newHotle= query.getResultList();
	       List<HotelDTO> hoteldto = new ArrayList<>();
	       for(Hotel h: newHotle) {
	    	   hoteldto.add(convertToDTO(h));
	       }
	       return hoteldto;
	   }
	   
	   public static List<HotelDTO> convertToDTOList(List<Hotel> hotelList) {
	       List<HotelDTO> hoteldtoList = new ArrayList<HotelDTO>();
	       for(Hotel h: hotelList){
	    	   hoteldtoList.add(convertToDTO(h));
	       }
	       return  hoteldtoList;
	}   
	   
	   
	    public String getPrincipalName() {
	        return context.getCallerPrincipal().getName();
	    }
	    
	    public static  HotelDTO convertToDTO(Hotel hotel)
	    {
	    	HotelDTO newHoteldto=new HotelDTO();
	    	newHoteldto.sethID(hotel.getHID());
	    	newHoteldto.sethCity(hotel.getHCity());
	    	newHoteldto.sethCost(hotel.getHCost());
	    	newHoteldto.sethName(hotel.getHName());

	    	
	    	return newHoteldto;
	    	
	    }


}
